//
//  ModelGroupListInfo.m
//  宝宝儿歌
//
//  Created by student on 16/4/23.
//  Copyright © 2016年 baobaoerge. All rights reserved.
//

#import "ModelGroupListInfo.h"
#import <AFNetworking.h>
#import <TFHpple.h>


@implementation ModelGroupListInfo

//
+ (void)modelGroupListInfo:(NSString *)urlStr
                    successBlok:(void (^)(NSArray *imgList))successBlock
                       failBlok:(void (^)(NSError *error))failBlock
{
    NSString *str = @"http://www.ximalaya.com/dq/kid-%E5%84%BF%E6%AD%8C%E5%A4%A7%E5%85%A8/";

    NSURL *url = [NSURL URLWithString:urlStr];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError)
     {
         if (!connectionError)
         {
             
             TFHpple *hpple = [TFHpple hppleWithHTMLData:data];
             
             
             NSString *xpath1 = @"//div [@class='discoverAlbum_item']/a";//链接和title
             NSString *xpath2 = @"//div [@class='discoverAlbum_item']/div/a/span/img";//图片
             NSArray *arr1 = [hpple searchWithXPathQuery:xpath1];
             NSArray *arr2 = [hpple searchWithXPathQuery:xpath2];
             
             NSMutableArray *list1 = [NSMutableArray array];
             NSMutableArray *list2 = [NSMutableArray array];
             
             for (TFHppleElement *element in arr1) {
                 NSMutableDictionary *tmpDict = [NSMutableDictionary dictionary];
                 NSDictionary *dict = element.attributes;
                 NSString *link = dict[@"href"];
                 NSString *title = element.text;
                 [tmpDict setValue:link forKey:@"radioGroupLink"];
                 [tmpDict setValue:title forKey:@"radioGroupTitle"];
                 if (tmpDict.allKeys.count >1) {
                     [list1 addObject:tmpDict];
                 }
                 
                 
             }
             for (int i = 0; i<list1.count; i++) {
                 TFHppleElement *element = arr2[i];
                 NSDictionary *dict = element.attributes;
                 NSDictionary *tmpDict = list1[i];
                 NSString *image = dict[@"src"];
                 [tmpDict setValue:image forKey:@"radioGroupImage"];
                 [list2 addObject:tmpDict];

             }
             successBlock(list2);
             
         }
         else
         {
             NSLog(@"请求失败");
             failBlock(nil);
         }
         
     }];
}

@end
